import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_animate/flutter_animate.dart';
import 'package:get/get.dart';
import 'package:ball_lion/entity/match/zq_match_info_entity.dart';
import 'package:ball_lion/entity/match/match_text_live_entity.dart';
import 'package:ball_lion/resources/colours.dart';
import 'package:ball_lion/module/match_detail/soccer/zq_nodata_widget.dart';

import 'package:ball_lion/resources/styles.dart';
import 'package:ball_lion/utils/utils.dart';
import 'package:ball_lion/widgets/base/wqs_image.dart';
import 'package:ball_lion/widgets/base/wqs_text.dart';

class ZqTextLiveWidget extends StatelessWidget {
  const ZqTextLiveWidget({super.key, this.texts = const [], this.info});

  final List<MatchLiveTextEntity> texts;

  // final detail = Get.find<SoccerMatchDetailController>();

  final ZqMatchInfoEntity? info;

  @override
  Widget build(BuildContext context) {
    if (texts.isEmpty) {
      return ZqNodataWidget(info: info);
    }
    return Stack(
      children: [
        Positioned(
            left: 21.5,
            top: 20,
            bottom: 36,
            child: Container(color: Colours.greyCC, width: 1)),
        Column(children: [
          const SizedBox(height: 10),
          ...List.generate(texts.length, (index) {
            final e = texts[index];
            return e.isAnimated
                ? item(e, index == texts.length - 1)
                : item(e, index == texts.length - 1)
                    .animate(
                      key: Key("${e.processTime} ${e.content} ${index}"),
                      onComplete: (controller) {
                        e.isAnimated = true;
                      },
                    )
                    .fadeIn(duration: 500.ms, begin: 0);
          })
        ]),
      ],
    );
  }

  Widget item(MatchLiveTextEntity text, bool noLine) {
    String time = text.processTime ?? "";
    if (!time.endsWith("'") && time.isNotEmpty) {
      time += "'";
    }
    return Row(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Container(
          width: 44,
          padding: const EdgeInsets.symmetric(vertical: 2),
          margin: const EdgeInsets.only(bottom: 8),
          color: Colours.white,
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              if (text.textType == 0)
                Container(
                    width: 44,
                    height: 20,
                    color: Colors.white,
                    child: Image.asset(Utils.getImgPath("text_live_host.png"),
                        width: 24))
              else if (text.event?.asset != null)
                Image.asset(
                    width: 16,
                    height: 16,
                    Utils.getImgPath(text.event!.asset!)),
              Container(height: 2, color: Colours.white),
              Flexible(
                child: Container(
                  width: 1,
                  color: noLine ? Colours.transparent : Colours.greyCC,
                ),
              )
            ],
          ),
        ),
        Expanded(
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Row(
                children: [
                  QsText(
                    time,
                    color: Colours.grey99,
                    fontSize: 12,
                  ),
                  const Spacer(),
                  if (text.textType == 1)
                    QsImage(
                      height: 16,
                      fit: BoxFit.cover,
                      imageUrl: info?.baseInfo?.homeLogo,
                      placeholder: Styles.placeholderIcon(),
                      errorWidget: Image.asset(
                        Utils.getImgPath('zq_placeholder.png'),
                        height: 16,
                        width: 16,
                      ),
                    )
                  else if (text.textType == 2)
                    QsImage(
                      height: 16,
                      fit: BoxFit.cover,
                      imageUrl: info?.baseInfo?.guestLogo,
                      placeholder: Styles.placeholderIcon(),
                      errorWidget: Image.asset(
                        Utils.getImgPath('zq_placeholder.png'),
                        height: 16,
                        width: 16,
                      ),
                    )
                ],
              ).paddingSymmetric(vertical: 2),
              Container(
                width: Get.width,
                padding: const EdgeInsets.all(8),
                decoration: BoxDecoration(
                    color: Colours.greyF7,
                    borderRadius: BorderRadius.circular(8)),
                child: QsText(
                  text.content ?? "",
                  fontSize: 14,
                  maxLines: 10,
                ),
              ),
              Container(height: 8)
            ],
          ),
        ),
        Container(width: 12)
      ],
    );
  }
}
